﻿using Core.Utility.Helper;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dto.Core
{
    /// <summary>
    /// 分页器
    /// </summary>
    public class PagerDto:BaseDto
    {
        #region 分页大小

        /// <summary>
        /// 分页大小
        /// </summary>
        private int _pageSize = 30;

        /// <summary>
        /// 分页大小
        /// </summary>
        [JsonProperty("page_size")]
        public int PageSize
        {
            get { return _pageSize; }
            set 
            { 
                SetProperty(ref _pageSize, value);
                SetPageCount();
            }
        }

        #endregion 分页大小 

        #region 页面数据总数

        /// <summary>
        /// 页面数据总数
        /// </summary>
        private int _pageDataTotal;

        /// <summary>
        /// 页面数据总数
        /// </summary>
        [JsonProperty("page_data_total")]
        public int PageDataTotal
        {
            get { return _pageDataTotal; }
            set 
            { 
                SetProperty(ref _pageDataTotal, value);
                SetPageCount();
            }
        }

        #endregion 页面数据总数 

        #region 页面总数

        /// <summary>
        /// 页面总数
        /// </summary>
        private int _pageCount = 0;

        /// <summary>
        /// 页面总数
        /// </summary>
        [JsonProperty("page_count")]
        public int PageCount
        {
            get { return _pageCount; }
            protected set
            {
                SetProperty(ref _pageCount, value);
            }
        }

        #endregion 页面总数 

        #region 分页索引(非当前页码，当前页码-1)

        /// <summary>
        /// 分页索引(非当前页码，当前页码-1)
        /// </summary>
        private int _pageIndex = 0;

        /// <summary>
        /// 分页索引(非当前页码，当前页码-1)
        /// </summary>
        [JsonProperty("page_index")]
        public int PageIndex
        {
            get { return _pageIndex; }
            set 
            {
                SetProperty(ref _pageIndex, value);
                if (_pageNumber != value + 1)
                {
                    PageNumber = value + 1;
                }
            }
        }

        #endregion 分页索引(非当前页码，当前页码-1) 

        #region 当前页码

        /// <summary>
        /// 当前页码
        /// </summary>
        private int _pageNumber;

        /// <summary>
        /// 当前页码
        /// </summary>
        [JsonProperty("page_number")]
        public int PageNumber
        {
            get { return _pageNumber; }
            set 
            { 
                SetProperty(ref _pageNumber, value); 
                if(_pageIndex != value -1)
                {
                    PageIndex = value - 1;
                }
            }
        }

        #endregion 当前页码 


        /// <summary>
        /// 设置页面总数
        /// </summary>
        private void SetPageCount()
        {
            if (PageSize > 0)
            {
                int pageCount = PageDataTotal / PageSize;
                if (PageDataTotal % PageSize > 0)
                {
                    pageCount++;
                }
                if (_pageCount != pageCount)
                {
                    PageCount = pageCount;
                }
            }
            else
            {
                PageCount = 0;
            }
            if(PageIndex > PageCount)
            {
                PageIndex = PageCount;
            }
        }
    }
}
